package com.itheima.web.listener;

import com.itheima.autoconfig.huawei.HuaWeiUGCTemplate;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@RocketMQMessageListener(topic = "tanhua-movement",consumerGroup = "movement-consumer-group")
public class MovementCheckListener implements RocketMQListener<String> {

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Reference
    private MovementService movementService;

    @Override
    public void onMessage(String movementId) {

        System.out.println("接收到了要审核的动态id" + movementId);

        //根据动态id查询动态
        Movement movement = movementService.findMyMovementById(movementId);

        //对动态文字和图片进行审核
        if(movement.getState()==0){
            //审核文本
            boolean textContentCheck = huaWeiUGCTemplate.textContentCheck(movement.getTextContent());

            //审核图片
            boolean imageContentCheck = huaWeiUGCTemplate.imageContentCheck(movement.getMedias().toArray(new String[]{}));

            Integer state = 2;//默认，已经驳回
            if (textContentCheck && imageContentCheck) {
                state = 1;
            }

            //3. 调整动态的状态
            movementService.updateMovementState(movementId, state);
        }
    }
}
